package vues.artiste;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.Document;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTable;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class Recherche extends JFrame implements DocumentListener{
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JLabel lblNom;
	private JTextField txtNom;
	private JLabel lblNumero;
	private JTextField txtNumero;
	private JLabel lblResultat;
	private JButton btnRetour;
	private JScrollPane scrollResultat;
	private RowFilter<TableModel, Integer> filter;
	private modeles.Artistes artistes = new modeles.Artistes();
	private TableRowSorter<TableModel> sorter;
	private JTable tableResultat;

	/**
	 * Create the frame.
	 */
	public Recherche() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Recherche.class.getResource("/images/Artiste.png")));
		setTitle("Recherche");
		setAutoRequestFocus(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 454, 341);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNom());
		contentPane.add(getTxtNom());
		contentPane.add(getLblNumero());
		contentPane.add(getTxtNumero());
		contentPane.add(getScrollResultat());
		contentPane.add(getLblResultat());
		contentPane.add(getBtnRetour());
		getRootPane().setDefaultButton(btnRetour);
	}
	private JLabel getLblNom() {
		if (lblNom == null) {
			lblNom = new JLabel("Nom : ");
			lblNom.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblNom.setBounds(10, 38, 51, 26);
		}
		return lblNom;
	}
	private JTextField getTxtNom() {
		if (txtNom == null) {
			txtNom = new JTextField();
			txtNom.setFont(new Font("Andalus", Font.PLAIN, 18));
			txtNom.setColumns(10);
			txtNom.setBounds(65, 41, 261, 20);
			txtNom.getDocument().addDocumentListener(this);
		}
		return txtNom;
	}
	private JLabel getLblNumero() {
		if (lblNumero == null) {
			lblNumero = new JLabel("Num\u00E9ro :");
			lblNumero.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblNumero.setBounds(10, 8, 107, 26);
		}
		return lblNumero;
	}
	private JTextField getTxtNumero() {
		if (txtNumero == null) {
			txtNumero = new JTextField();
			txtNumero.setFont(new Font("Andalus", Font.PLAIN, 18));
			txtNumero.setColumns(10);
			txtNumero.setBounds(87, 11, 239, 20);
			txtNumero.getDocument().addDocumentListener(this);
		}
		return txtNumero;
	}
	private JLabel getLblResultat() {
		if (lblResultat == null) {
			lblResultat = new JLabel("R\u00E9sultat : ");
			lblResultat.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblResultat.setBounds(10, 112, 89, 14);
		}
		return lblResultat;
	}
	private JButton getBtnRetour() {
		if (btnRetour == null) {
			btnRetour = new JButton("Retour");
			btnRetour.setIcon(new ImageIcon(Recherche.class.getResource("/images/Boutons/Retour.png")));
			btnRetour.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnRetour.setBounds(336, 269, 102, 23);
			btnRetour.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnRetour.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return btnRetour;
	}
	private JTable getTableResultat() {
		if (tableResultat == null) {
			artistes.update();
			tableResultat = new JTable(artistes);
			tableResultat.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			sorter = new TableRowSorter<TableModel>(artistes);
			filter = new RowFilter<TableModel, Integer>() {
				@Override
				public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
					String numero = ((String)("" + entry.getValue(0))).toUpperCase();
					String nom = ((String)entry.getValue(1)).toUpperCase();
					String numeroR = null;
					String nomR = null;
					if(txtNumero.getText().length() > 0){
						numeroR = txtNumero.getText().toUpperCase();
					}
					if(txtNom.getText().length() > 0){
						nomR = txtNom.getText().toUpperCase();
					}
					if((numeroR == null || numero.contains(numeroR)) && (nomR == null || nom.contains(nomR))){
						return true;
					}
					return false;
				}
			};
			sorter.setRowFilter(filter);
			tableResultat.setRowSorter(sorter);
			tableResultat.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		return tableResultat;
	}
	private JScrollPane getScrollResultat() {
		if (scrollResultat == null) {
			scrollResultat = new JScrollPane(getTableResultat());
			scrollResultat.setBounds(10, 137, 316, 155);
		}
		return scrollResultat;
	}
	private void update(){
		sorter.sort();
	}
	@Override
	public void changedUpdate(DocumentEvent e) {
		modification(e.getDocument());
	}
	@Override
	public void insertUpdate(DocumentEvent e) {
		modification(e.getDocument());
	}
	@Override
	public void removeUpdate(DocumentEvent e) {
		modification(e.getDocument());
	}
	
	public void modification(Document e){
		update();
	}
	
}
